package com.xtourms.client.dep;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.dep.hotel.HotelDep;
import com.xtourms.client.dep.product.ProductDep;
import com.xtourms.client.dep.ticket.TicketDep;
import com.xtourms.shared.field.WorkerField;

public class DepartmentFrame extends Composite {

	private static DepartmentUiBinder uiBinder = GWT
			.create(DepartmentUiBinder.class);

	interface DepartmentUiBinder extends UiBinder<Widget, DepartmentFrame> {
	}

	private Widget cur_res;
	private Anchor cur_nav;
	
	@UiField HTMLPanel depFrame;
	@UiField Anchor dep_hotel;
	@UiField Anchor dep_ticket;
	@UiField Anchor dep_travel;
	@UiField Anchor dep_product;
	@UiField HTMLPanel nothing;
	
	public DepartmentFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		cur_res = nothing;
		initMyDep();
	}

	private void initMyDep() {
		int myDep = (int) ((JSONNumber)Context.ins.worker.get(WorkerField.WORKER_DEP)).doubleValue();
		
		switch(myDep){
		case 1:
			switchDep(dep_hotel, new HotelDep());
			break;
		case 2:
			switchDep(dep_hotel, new HotelDep());
			dep_ticket.setVisible(false);
			dep_travel.setVisible(false);
			dep_product.setVisible(false);
			break;
		case 3:
			dep_hotel.setVisible(false);
			switchDep(dep_ticket, new TicketDep());
			dep_travel.setVisible(false);
			dep_product.setVisible(false);
			break;
		case 4:
			dep_hotel.setVisible(false);
			dep_ticket.setVisible(false);
			//switchDep(dep_travel, new TravelDep());
			dep_product.setVisible(false);
			break;
		case 5:
			dep_hotel.setVisible(false);
			dep_ticket.setVisible(false);
			dep_travel.setVisible(false);
			switchDep(dep_product, new ProductDep());
			break;
		default:
			break;
		}
	}

	private void switchDep(Anchor nextNav, Widget nextRm) {
		if(cur_nav != nextNav){
			if(null != cur_nav){
				cur_nav.removeStyleName("cur_sub_nav");
			}
			cur_nav = nextNav;
			cur_nav.addStyleName("cur_sub_nav");
			cur_res.removeFromParent();
			cur_res = nextRm;
			depFrame.add(cur_res, "dep_content");
		}
	}
	
	@UiHandler("dep_hotel")
	void manageHotel(ClickEvent evt){
		switchDep(dep_hotel, new HotelDep());
	}
	
	@UiHandler("dep_ticket")
	void manageTicket(ClickEvent evt){
		switchDep(dep_ticket, new TicketDep());
	}
	
	@UiHandler("dep_travel")
	void manageTravel(ClickEvent evt){
		//switchDep(dep_travel, new TravelDep());
	}
	
	@UiHandler("dep_product")
	void manageProduct(ClickEvent evt){
		switchDep(dep_product, new ProductDep());
	}
}
